package com.sdxxtop.robotproject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ainirobot.coreservice.client.Definition;
import com.ainirobot.coreservice.client.RobotApi;
import com.ainirobot.coreservice.client.listener.TextListener;
import com.bumptech.glide.Glide;
import com.sdxxtop.robotproject.adapter.GridAdapter;
import com.sdxxtop.robotproject.camera.CustomCameraActivity;
import com.sdxxtop.robotproject.control.MessageManager;
import com.sdxxtop.robotproject.global.App;
import com.sdxxtop.robotproject.presenter.iview.SkillView;
import com.sdxxtop.robotproject.skill.MoveSkill;
import com.sdxxtop.robotproject.skill.SpeechSkill;
import com.sdxxtop.robotproject.utils.FuzzyUtils;
import com.sdxxtop.robotproject.utils.MessageParser;
import com.sdxxtop.robotproject.widget.SiriView;

public class MainActivity extends BaseActivity implements SkillView,Handler.Callback {

    public static final String TAG = "MainActivity";

    private GridView gridview;
    private GridAdapter adapter;
    //    private MainPresenter mainPresenter;
    private View sleepLayout;
    private ImageView sleepImage;
    private SiriView siriNoSpeakingView;
    private SiriView siriSpeakingView;
    private TextView rainbowTextView;
    private MainActivity mContext;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = this;
//        if (!SystemUtils.isServiceRunning(ModuleService.class, getApplicationContext())) {
//            Log.d(TAG, "onCreate: ");
//            Intent intent = new Intent(this, ModuleService.class);
//            startService(intent);
//        }
        handler = new Handler(this);
        handler.sendEmptyMessage(100);

        initVariables();
        initView();
    }

    private void initView() {

        gridview = findViewById(R.id.gridview);
        adapter = new GridAdapter();
        gridview.setAdapter(adapter);

        adapter.setItemClickListener(new GridAdapter.ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = null;
                switch (position) {
                    case 0:
                        intent = new Intent(MainActivity.this, ChatActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        intent = new Intent(MainActivity.this, MoveActivity.class);
                        startActivity(intent);
                        break;
                    case 2:
                        intent = new Intent(MainActivity.this, CustomCameraActivity.class);
                        startActivity(intent);
                        break;
                    case 3:
                        intent = new Intent(MainActivity.this, SingleImageActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });


        sleepLayout = findViewById(R.id.sleep_layout);
        sleepImage = findViewById(R.id.sleep_image);
        siriNoSpeakingView = findViewById(R.id.siri_no_speaking_view);
        siriSpeakingView = findViewById(R.id.siri_speaking_view);
        rainbowTextView = findViewById(R.id.chat_text);

        Glide.with(this).load(R.drawable.main_normal).into(sleepImage);
    }

    private void initVariables() {

//        mainPresenter = new MainPresenter();

        //skill 是否连接成功
        if (App.getInstance().isRobotInitSuccess) {
            register();
        } else {
            App.getInstance().setRobotRunnable(new Runnable() {
                @Override
                public void run() {
                    register();
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        handler.sendEmptyMessage(100);
        App.getInstance().addView(this);
        MessageManager.getInstance().setPause(false);
//        mainPresenter.addView(this);
    }

    @Override
    protected void onPause() {
        handler.removeMessages(100);
        super.onPause();
        App.getInstance().removeView(this);
        MessageManager.getInstance().setPause(true);
//        mainPresenter.removeView();
    }

    private void register() {
//        robotPresenter.registerModule();
    }

    private void speaking(boolean speaking) {
        if (speaking) {
            siriSpeakingView.setVisibility(View.VISIBLE);
            siriNoSpeakingView.setVisibility(View.GONE);
        } else {
            siriSpeakingView.setVisibility(View.GONE);
            siriNoSpeakingView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onSpeechParResult(final String speechMessage) {
        Log.e(TAG, "onSpeechParResult thread " + Thread.currentThread());
        if (!TextUtils.isEmpty(speechMessage)) {
            Intent intent = null;
            if ("打开问答".equals(speechMessage) || "开启问答".equals(speechMessage)) {
                intent = new Intent(MainActivity.this, ChatActivity.class);
                startActivity(intent);
            } else if ("打开带路".equals(speechMessage) || "开启带路".equals(speechMessage)) {
                intent = new Intent(MainActivity.this, MoveActivity.class);
                startActivity(intent);
            } else if ("打开合影".equals(speechMessage) || "开启合影".equals(speechMessage)) {
                intent = new Intent(MainActivity.this, CustomCameraActivity.class);
                startActivity(intent);
            } else if ("打开美图".equals(speechMessage) || "开启美图".equals(speechMessage)) {
                intent = new Intent(MainActivity.this, SingleImageActivity.class);
                startActivity(intent);
            } else if (!TextUtils.isEmpty(speechMessage) && FuzzyUtils.contains2(speechMessage)) {
                String answerText = getString(R.string.report_luanshen);
                SpeechSkill.getInstance().playTxt(answerText);
            } else if (!TextUtils.isEmpty(speechMessage) && FuzzyUtils.contains3(speechMessage)) {
                String answerText = getString(R.string.report_go_position);
                SpeechSkill.getInstance().playTxt(answerText);
            } else if (!TextUtils.isEmpty(speechMessage) && FuzzyUtils.contains4(speechMessage)) {
                String answerText = getString(R.string.report_say_bye);
                SpeechSkill.getInstance().playTxt(answerText);
            } else if (!TextUtils.isEmpty(speechMessage) && FuzzyUtils.contains5(speechMessage)) {
                String answerText = getString(R.string.report_jieri);
                SpeechSkill.getInstance().playTxt(answerText);
            } else if (!TextUtils.isEmpty(speechMessage) && FuzzyUtils.contains6(speechMessage)) {
                String answerText = getString(R.string.report_luanshen_jieshu);
                SpeechSkill.getInstance().playTxt(answerText);
            } else if (!TextUtils.isEmpty(speechMessage) && FuzzyUtils.contains7(speechMessage)) {
                String answerText = getString(R.string.report_laoban);
                SpeechSkill.getInstance().playTxt(answerText);
            } else if (speechMessage.contains("带我去大门")) {
                SpeechSkill.getInstance().playTxt("正在去往大门，跟我来吧！", new TextListener() {
                    @Override
                    public void onComplete() {
                        super.onComplete();
                        MoveSkill.getInstance().goPosition(mContext, "大门");
                    }
                });
            } else if (speechMessage.contains("带我去讲解台")) {
                SpeechSkill.getInstance().playTxt("正在去往讲解台，跟我来吧！", new TextListener() {
                    @Override
                    public void onComplete() {
                        super.onComplete();
                        MoveSkill.getInstance().goPosition(mContext, "讲解台");
                    }
                });
            } else if (speechMessage.contains("带我去展厅")) {
                SpeechSkill.getInstance().playTxt("正在去往讲展厅，跟我来吧！", new TextListener() {
                    @Override
                    public void onComplete() {
                        super.onComplete();
                        MoveSkill.getInstance().goPosition(mContext, "展厅");
                    }
                });
            } else if (speechMessage.contains("退出导航")) {
                SpeechSkill.getInstance().playTxt("退出导航成功", new TextListener() {
                    @Override
                    public void onComplete() {
                        super.onComplete();
                        RobotApi.getInstance().stopGoPosition(0);
                    }
                });
            }

        }
        if (speechMessage.contains("小豹")) {
            String tempSpeechMessage = speechMessage.replace("小豹", "小伴");
            rainbowTextView.setText(tempSpeechMessage);
        } else {
            rainbowTextView.setText(speechMessage);
        }
    }

    @Override
    public void onStartSkill() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                speaking(true);
            }
        });

        Log.e(TAG, "onStartSkill thread " + Thread.currentThread());
    }

    @Override
    public void onStopSkill() {
        Log.e(TAG, "onStopSkill thread " + Thread.currentThread());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                speaking(false);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        rainbowTextView.setText("");
                    }
                }, 3000);


            }
        });
    }

    private void showSleepView() {
        String s = rainbowTextView.getText().toString();
        if (TextUtils.isEmpty(s)) {
            sleepLayout.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onVolumeChange(int volume) {
        Log.e(TAG, "onVolumeChange thread " + Thread.currentThread());
    }

    @Override
    public void onQueryEnded(int query) {
        Log.e(TAG, "onQueryEnded thread " + Thread.currentThread() + " query: " + query);
    }

    @Override
    public void onSendRequest(String reqType, String reqText, String reqParam) {
        String userText = MessageParser.getUserText(reqText);

        if (!TextUtils.isEmpty(userText) && "打开合影".equals(userText)) {
            Intent intent = new Intent(MainActivity.this, CustomCameraActivity.class);
            startActivity(intent);
            return;
        }
        sleepLayout.setVisibility(View.GONE);
        switch (reqType) {
            case Definition.REQ_SPEECH_WAKEUP:
                if (sleepLayout.getVisibility() == View.INVISIBLE) {
                    gridview.setAdapter(adapter);
                }
                sleepLayout.setVisibility(View.GONE);
                break;
            case Definition.REQ_SPEECH_SLEEP:

                break;
            case Definition.REQ_CHAT:
                Log.e(TAG, "onSendRequest" + Definition.REQ_CHAT);
                break;
            case Definition.REQ_TELL_ME_WHY:

                break;
            default:
                Log.e(TAG, "onSendRequest default ");
                break;
        }
    }

    @Override
    public void onComplete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                rainbowTextView.setText("");
            }
        });
    }

    @Override
    public boolean handleMessage(Message message) {
        switch (message.what) {
            case 100:
//                showSleepView();
                handler.sendEmptyMessageDelayed(100, 10000);
                break;
        }
        return true;
    }
}
